package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lifeihu
 * @date 2021/11/24
 * @TIME:11:32
 */
@Slf4j
@RestController
@RequestMapping("/category")
@Api(description = "分类管理API")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @ApiOperation("添加菜品或套餐分类")
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("category:{}",category);
        categoryService.save(category);
        return R.success("新增分类成功");
    }
    @GetMapping("/page")
    public R findByPage(Integer page,Integer pageSize){
        IPage<Category> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> wapper=new LambdaQueryWrapper<>();
        wapper.orderByAsc(Category::getSort);
        IPage<Category> page1 = categoryService.page(pageInfo, wapper);
        return R.success(page1);
    }
    @DeleteMapping
    public R removeCategoryById(Long id){
        categoryService.remove(id);
        return R.success("删除成功");
    }
    @PutMapping
    public R modify(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }
    @GetMapping("/list")
    public R<List<Category>>  list(Category category){
        LambdaQueryWrapper<Category> wapper = new LambdaQueryWrapper<>();
        wapper.eq(category.getType()!=null,Category::getType,category.getType());
        wapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(wapper);
        return R.success(list);
    }
}
